package main.java.pers.seg.model.utils;

import org.dom4j.Element;

import static main.java.pers.seg.model.constants.ObjectConstants.coremap;

public class StringUtils {

    private static final String sp = "\\s+";

    public static String[] cut(String str){
        String[] arr = str.split(sp);

        return arr;
    }

    public static String addBlank(String source,int count){
        String blank ="";
        for(int i=0;i<count;i++){
            blank +=" ";
        }
        return blank;
    }

    public static boolean isScreen(Element ifmlnode){
        if(ifmlnode.attributeValue("name").indexOf("Activity")!=-1 || ifmlnode.attributeValue("name").indexOf("Fragment")!=-1
                || ifmlnode.attributeValue("name").indexOf("Dialog")!=-1 || ifmlnode.attributeValue("name").equals("android.support.design.widget.NavigationView")){
            return true;

        }
        else if(ifmlnode.attributeValue("type").equals("ext:MenuList")){
            return true;
        }
        return false;
    }

    /**
     * 对type进行分类
     * @param ifmlElement
     * @param name
     * @param ce_type
     */
    public static void classifyType(Element ifmlElement, String name, String ce_type) {
        if (name.indexOf("icon") == -1) {
            ifmlElement.addAttribute("xsi:type", coremap.get(ce_type) + ":" + ce_type);
        } else {
            ifmlElement.addAttribute("xsi:type", coremap.get("Icon") + ":" + "Icon");
        }
    }

    /**
     * 处理id
     * @param gatornode
     * @param ifmlElement
     * @param type
     * @param name
     */
    public static void classifyId(Element gatornode, Element ifmlElement, String type, String name){
        if(gatornode.attribute("title") != null){
            String title = gatornode.attributeValue("title");
            //menuitem单独处理
            if(type.equals("android.view.MenuItem")){
                ifmlElement.addAttribute("id", name);
                ifmlElement.addAttribute("index", "");
                ifmlElement.addAttribute("name", name);
                ifmlElement.addAttribute("text", title);
            }
            else{
                ifmlElement.addAttribute("id", "");
                ifmlElement.addAttribute("index", "");
                ifmlElement.addAttribute("name", title);
                ifmlElement.addAttribute("text", title);
            }

        }
        else{
            if(!name.equals("NO_ID")){
                ifmlElement.addAttribute("id", name);
                ifmlElement.addAttribute("name", name);
            }
            else{
                ifmlElement.addAttribute("id", "");
                ifmlElement.addAttribute("index", "");
                ifmlElement.addAttribute("text", "");
            }
        }
    }



}
